#!/bin/env python
#coding:utf-8
import sys
__author__ = 'jieli'

from django.db import models
from . import auth
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe




# class UserProfile(auth.AbstractBaseUser, auth.PermissionsMixin):
#     email = models.EmailField(
#         verbose_name='email address',
#         max_length=255,
#         unique=True,
#
#     )
#     # password = models.CharField(_('password'), max_length=128,
#     #                             help_text=mark_safe('''<a class='btn-link' href='password'>重置密码</a>'''))
#     phone = models.BigIntegerField(_('phone'),blank=True,null=True)
#     weixin = models.CharField(_('weixin'),max_length=64,blank=True,null=True)
#     is_active = models.BooleanField(_('active'),default=True)
#     is_admin = models.BooleanField(_('admin'),default=False)
#     is_staff = models.BooleanField(
#         verbose_name='staff status',
#         default=True,
#         help_text='Designates whether the user can log into this admin site.',
#     )
#     #name = models.CharField(max_length=32)
#     name = models.CharField(_('name'),max_length=32, unique=True)
#     #role = models.ForeignKey("Role",verbose_name="权限角色")
#
#     memo = models.TextField('备注', blank=True, null=True, default=None)
#     date_joined = models.DateTimeField(blank=True, null=True, auto_now_add=True)
#
#     USERNAME_FIELD = 'email'
#     # REQUIRED_FIELDS = ['name','token','department','tel','mobile','memo']
#     REQUIRED_FIELDS = ['name']
#
#     def get_full_name(self):
#         # The user is identified by their email address
#         return self.email
#
#     def get_short_name(self):
#         # The user is identified by their email address
#         return self.email
#
#     def __str__(self):  # __str__ on Python 2
#         return self.email
#
#     def has_perms(self, perm, obj=None):
#         return True
#
#     def has_module_perms(self, app_label):
#         "Does the user have permissions to view the app `app_label`?"
#         # Simplest possible answer: Yes, always
#         return True
#
#
#     @property
#     def is_superuser(self):
#         "Is the user a member of staff?"
#         # Simplest possible answer: All admins are staff
#         return self.is_admin
#
#
#     objects = auth.UserManager()
#
#     class Meta:
#         verbose_name = '账户'
#         verbose_name_plural = '账户'




# #通过继承AbstractUser类的方式扩展用户   ——success
#from django.contrib.auth.models import AbstractUser
# class MasonUser(AbstractUser):
#     nickname = models.CharField(max_length=50, blank=True)
#     class Meta(AbstractUser.Meta):
#         pass


#来源互联网 https://blog.csdn.net/wangtaoking1/article/details/50187589   ——success
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser,PermissionsMixin
class MyUserManager(BaseUserManager):
    def _create_user(self, name, email, password, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        if not name:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(name=name, username=name, email=email, **extra_fields)
        user.set_password(password)
        user.is_superuser = True
        user.save(using=self._db)
        return user

    def create_user(self, name, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        return self._create_user(name, email, password, **extra_fields)

    def create_superuser(self, name, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True')

        return self._create_user(name, email, password, **extra_fields)


class MyUser(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=254, unique=True, db_index=True)
    name = models.CharField(max_length=254, unique=True, db_index=True)
    email = models.EmailField('email address', max_length=254, unique=True)


    is_staff = models.BooleanField('staff status', default=False)
    is_active = models.BooleanField('active', default=True)

    #mason add
    phone = models.BigIntegerField(_('phone'),blank=True,null=True)
    weixin = models.CharField(_('weixin'),max_length=64,blank=True,null=True)
    #is_active = models.BooleanField(_('active'),default=True)
    is_admin = models.BooleanField(_('admin'),default=False)
    # is_staff = models.BooleanField(
    #     verbose_name='staff status',
    #     default=True,
    #     help_text='Designates whether the user can log into this admin site.',
    # )
    #name = models.CharField(max_length=32)
    #name = models.CharField(_('name'),max_length=32, unique=True)
    #role = models.ForeignKey("Role",verbose_name="权限角色")
    memo = models.TextField('备注', blank=True, null=True, default=None)
    date_joined = models.DateTimeField(blank=True, null=True, auto_now_add=True)


    #USERNAME_FIELD = 'username'
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    objects = MyUserManager()

    class Meta:
        db_table = 'myuser'

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username